package singleton;

//DCL
public class Singleton {
    //保证可见性,避免new 指令重排序
/*    private static volatile Singleton singleton = null;

    private Singleton(){
    }

    public static Singleton getSingleton(){
        //在加锁前判断,避免加锁后的性能消耗
        if( singleton == null ){
            synchronized (Singleton.class){
                //存在两个线程同时进入同步区块的可能,
                if ( singleton == null ){
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }*/
    private static volatile Singleton singleton = null;

    public Singleton() {
    }

    public static Singleton getSingleton(){
        if (singleton == null){
            synchronized (Singleton.class){
                if (singleton == null){
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}


